package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Menu;
import com.qf.enums.Page;
import com.qf.mapper.MenuMapper;
import com.qf.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 2019/12/11
 * 郭程鹏
 * springboot_oa_five
 * 面向代码面向君,不负时光不负卿
 */
@Service
public class MenuServiceImpl implements MenuService {


    @Autowired
    private MenuMapper menuMapper;

    //异步校验权限码
    @Override
    public Integer ajaxPreCode(String preCode) {
        Integer ajaxPreCode = menuMapper.ajaxPreCode(preCode);
        return ajaxPreCode;
    }

    //异步校验请求路径
    @Override
    public Integer ajaxUrl(String menuUrl) {
        Integer ajaxUrl = menuMapper.ajaxUrl(menuUrl);
        return ajaxUrl;
    }

    //查询所有菜单那
    @Override
    public PageInfo<Menu> findAllMenu(Integer page,String menuName) {

        PageHelper.startPage(page,Page.pageSize.getSize());
        List<Menu> menu = menuMapper.findMenu(menuName);
        for (Menu menu1 : menu) {
            if (menu1.getSuperMenuId()==0){
                menu1.setSperMenuName("顶级菜单");
            }else {
                String father = menuMapper.findMenuById(menu1.getSuperMenuId());
                menu1.setSperMenuName(father);
            }
        }
        PageInfo<Menu> pageInfo = new PageInfo<>(menu);
        return pageInfo;
    }

    //查询顶级菜单
    @Override
    public List<Menu> findFatherMenu() {

        List<Menu> fatherMenu = menuMapper.findFatherMenu();
        return fatherMenu;
    }

    //异步校验用户名
    @Override
    public Integer ajaxMenuName(String menuName) {
        Integer name = menuMapper.ajaxMenuName(menuName);
        return name;
    }

    //添加菜单
    @Transactional
    @Override
    public Integer addMenu(Menu menu) {
        Integer integer = menuMapper.addMenu(menu);
        return menu.getMenuId();
    }

    //查询菜单信息
    @Override
    public Menu findMyMenuById(Integer menuId) {
        Menu myMenuById = menuMapper.findMyMenuById(menuId);
        Integer superMenuId = myMenuById.getSuperMenuId();
        if (superMenuId==0){
            myMenuById.setSperMenuName("顶级菜单");
        }else {
            Menu menu = menuMapper.findMyMenuById(superMenuId);
            myMenuById.setSperMenuName(menu.getMenuName());
        }
        myMenuById.setSperMenuName(myMenuById.getSperMenuName());
        return myMenuById;
    }

    //修改菜单
    @Transactional
    @Override
    public void updateMenu(Menu menu) {
        menuMapper.updateMenu(menu);
    }

    //检查是否可以删除菜单
    @Override
    public List<Integer> checkMenuInMiddle(Integer menuId) {
        List<Integer> checkMenuInMiddle = menuMapper.checkMenuInMiddle(menuId);
        return checkMenuInMiddle;
    }

    //删除菜单和中间表
    @Override
    @Transactional
    public void deleteMenuIdInMiddleFromMiddel(Integer menuId) {
        menuMapper.deleteMenu(menuId);
        menuMapper.deleteMenuIdInMiddle(menuId);
    }

    //禁用菜单
    @Override
    @Transactional
    public void menuBan(Integer menuId) {
        menuMapper.menuBan(menuId);
    }

    //启用菜单
    @Override
    @Transactional
    public void menuActive(Integer menuId) {
        menuMapper.menuActive(menuId);
    }

    //批量导出
    @Override
    public List<Menu> doExcle(List<Integer> menuId) {

        List<Menu> menus = menuMapper.doExcle(menuId);
        for (Menu menu : menus) {
            if (menu.getSuperMenuId()==0){
                menu.setSperMenuName("顶级菜单");
            }else {
                String father = menuMapper.findMenuById(menu.getSuperMenuId());
                menu.setSperMenuName(father);
            }
        }
        return menus;
    }

    //判断菜单是否可删
    @Override
    public List<Integer> checkMenuInMiddleDelete(List<Integer> menuId) {
        ArrayList<Integer> sum = new ArrayList<>();
        for (Integer id : menuId) {
            List<Integer> list = menuMapper.checkMenuInMiddle(id);
            if (list.size()>0){
                sum.add(id);
            }

        }
        return sum;
    }

    //批量删除
    @Transactional
    @Override
    public void deleteAll(List<Integer> menuId) {
        for (Integer id : menuId) {
            menuMapper.deleteAll(id);
        }
    }


}
